package com.songlanyun.modules.account.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.account.dao.UserServiceAreaTemporaryDao;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.account.entity.UserServiceAreaTemporaryEntity;
import com.songlanyun.modules.account.model.dto.SelectAreaDTO;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.account.service.UserServiceAreaTemporaryService;
import com.songlanyun.modules.exception.MemberException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("userServiceAreaTemporaryService")
public class UserServiceAreaTemporaryServiceImpl extends ServiceImpl<UserServiceAreaTemporaryDao, UserServiceAreaTemporaryEntity> implements UserServiceAreaTemporaryService {

    @Resource
    private AccountService accountService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserServiceAreaTemporaryEntity> page = this.page(
                new Query<UserServiceAreaTemporaryEntity>().getPage(params),
                new QueryWrapper<UserServiceAreaTemporaryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void updateServiceArea(Long userId, List<SelectAreaDTO> dto) {
        //清理用户的临时关系
        this.lambdaUpdate().eq(UserServiceAreaTemporaryEntity::getUserId, userId).remove();

        //保存
        ArrayList<UserServiceAreaTemporaryEntity> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(dto) || dto.size() > 0) {
            dto.forEach(a -> {
                UserServiceAreaTemporaryEntity entity = new UserServiceAreaTemporaryEntity();
                BeanUtil.copyPropertiesIgnoreNull(a, entity);
                entity.setUserId(userId);
                list.add(entity);
            });
        }
        this.saveBatch(list);
    }

}
